package com.cvv.controller;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.cvv.dao.LorderMapper;
import com.cvv.dao.StationMapper;
import com.cvv.entity.Lorder;
import com.cvv.entity.Order;
import com.google.gson.Gson;
import com.sun.org.apache.bcel.internal.generic.RETURN;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.converter.json.GsonBuilderUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import sun.security.util.Length;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/lorder")
public class LorderController {
    @Autowired
    private LorderMapper lorderMapper;
    @Autowired
    private StationMapper stationMapper;

    @RequestMapping(value = "selectByKey")
    @ResponseBody
    public String selectByKey(String id) {
        Lorder lorder = lorderMapper.selectByPrimaryKey(Integer.parseInt(id));
        String json = JSON.toJSONString(lorder);
        return json;
    }

    @RequestMapping(value = "selectAllFour", method = RequestMethod.GET)
    @ResponseBody
    public String selectAllFour() {
        List<Lorder> list = lorderMapper.selectAll();
        JSONArray jsonArray = (JSONArray) JSONArray.toJSON(list);
        String json = jsonArray.toString();
        return json;
    }

    @RequestMapping(value = "selectAll", method = RequestMethod.GET)
    @ResponseBody
    public String selectAll() {
        List<Lorder> list = lorderMapper.selectAlltwo();
        JSONArray jsonArray = (JSONArray) JSONArray.toJSON(list);
        String json = jsonArray.toString();
        return json;
    }

    @RequestMapping(value = "select", method = RequestMethod.GET)
    @ResponseBody
    public String select(String start, String end, String choice) {
        List<Lorder> list = lorderMapper.selectAlltwo();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        Date startDate = null;
        Date endDate = null;
        Date date = null;
        if (start.equals("")) {
            start = "0001-01-01";
        }
        if (end.equals("")) {
            end = "9999-12-30";
        }
        try {
            startDate = formatter.parse(start);
            endDate = formatter.parse(end);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        String check = checkChoice(choice);
        for (int i = 0; i < list.size(); i++) {
            Lorder lorder = list.get(i);
            try {
                date = formatter.parse(lorder.getRequireddate());
            } catch (ParseException e) {
                e.printStackTrace();
            }
            if (!isEffectiveDate(date, startDate, endDate)) {
                list.remove(lorder);
                i--;
            } else if (!check.equals("0")) {
                if (!lorder.getOrdertype().equals(check)) {
                    list.remove(lorder);
                    i--;
                }
            }
        }
        JSONArray jsonArray = (JSONArray) JSONArray.toJSON(list);
        String json = jsonArray.toString();
        return json;
    }

    @RequestMapping(value = "selectByLorder", method = RequestMethod.GET)
    @ResponseBody
    public String selectByLorder(String odd, String state, String orderId, String orderType, String consumerName, String station, String start, String end) {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        Date startDate = null;
        Date endDate = null;
        Date date = null;
        orderType = checkChoice(orderType);
        if (start.equals("")) {
            start = "0001-01-01";
        }
        if (end.equals("")) {
            end = "9999-12-30";
        }
        try {
            startDate = formatter.parse(start);
            endDate = formatter.parse(end);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        List<Lorder> list = lorderMapper.selectAll();
        for (int i = 0; i < list.size(); i++) {
            Lorder lorder = list.get(i);
            try {
                date = formatter.parse(lorder.getRequireddate());
            } catch (ParseException e) {
                e.printStackTrace();
            }
            if (!isEffectiveDate(date, startDate, endDate)) {
                list.remove(lorder);
                i--;
            } else if (!odd.equals("") && !(lorder.getOrder().getOrder() == Integer.parseInt(odd))) {
                list.remove(lorder);
                i--;
            } else if (!state.equals("全部") && !(lorder.getOrderstate().equals(state))) {
                list.remove(lorder);
                i--;
            } else if (!orderId.equals("") && !(lorder.getOrderid() == Integer.parseInt(orderId))) {
                list.remove(lorder);
                i--;
            } else if (!orderType.equals("0") && !(lorder.getOrdertype().equals(orderType))) {
                list.remove(lorder);
                i--;
            } else if (!consumerName.equals("") && !(lorder.getLcustomer().getCustomername().equals(consumerName))) {
                list.remove(lorder);
                i--;
            } else if (!(lorder.getDeliversubstation() == (stationMapper.selectByStationName(station)))) {
                list.remove(lorder);
                i--;
            }
        }
        JSONArray jsonArray = (JSONArray) JSONArray.toJSON(list);
        String json = jsonArray.toString();
        return json;
    }
    @RequestMapping(value = "updateState", method = RequestMethod.POST)
    @ResponseBody
    public String updateState(String orderid) {
        Lorder lorder = new Lorder();
        lorder.setOrderstate("有货");
        lorder.setOrderid(Integer.parseInt(orderid));
        if (lorderMapper.updateByPrimaryKeySelective(lorder) >= 1) {
            return "1";
        } else {
            return "修改失败!";
        }
    }
    @RequestMapping(value = "update", method = RequestMethod.POST)
    @ResponseBody
    public String update(String deliversubstation, String orderid) {
        Lorder lorder = new Lorder();
        lorder.setDeliversubstation(stationMapper.selectByStationName(deliversubstation));
        lorder.setOrderid(Integer.parseInt(orderid));
        if (lorderMapper.updateByPrimaryKeySelective(lorder) >= 1) {
            return "修改成功！";
        } else {
            return "修改失败!";
        }
    }

    private String checkChoice(String choice) {
        if (choice.equals("新订"))
            return "1";
        else if (choice.equals("换货"))
            return "2";
        else if (choice.equals("退货"))
            return "3";
        else if (choice.equals("退订"))
            return "4";
        else if (choice.equals("货到付款"))
            return "5";
        else if (choice.equals("全部"))
            return "0";
        else
            return "6";
    }

    private static boolean isEffectiveDate(Date nowTime, Date startTime, Date endTime) {
        if (nowTime.getTime() == startTime.getTime()
                || nowTime.getTime() == endTime.getTime()) {
            return true;
        }
        Calendar date = Calendar.getInstance();
        date.setTime(nowTime);
        Calendar begin = Calendar.getInstance();
        begin.setTime(startTime);
        Calendar end = Calendar.getInstance();
        end.setTime(endTime);
        if (date.after(begin) && date.before(end)) {
            return true;
        } else {
            return false;
        }
    }
}
